package com.object.module.cbb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.configuration.utils.PageUtils;
import com.object.configuration.utils.Query;
import com.object.dao.cbb.CbbOrderDao;
import com.object.module.cbb.entity.CbbOrderEntity;
import com.object.module.cbb.entity.CddShopEntity;
import com.object.module.cbb.service.CbbOrderService;
import com.object.module.cbb.service.CbbSortService;
import com.object.module.cbb.service.CbbUserService;
import com.object.module.cbb.service.CddShopService;
import org.apache.commons.lang.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service("cbbOrderService")
public class CbbOrderServiceImpl extends ServiceImpl<CbbOrderDao, CbbOrderEntity> implements CbbOrderService {
    @Autowired
    private CddShopService shopService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private Redisson redisson;
    @Autowired
    private CbbSortService cbbSortService;
    @Autowired
    private CbbUserService cbbUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CbbOrderEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            //订单号
            wrapper.eq("or_uuid", key);
        }
        IPage<CbbOrderEntity> page = this.page(
                new Query<CbbOrderEntity>().getPage(params),
                wrapper
        );


        return new PageUtils(page);
    }


    @Override
    public List<CbbOrderEntity> allPhone(Long userId, String status, String key) {
        QueryWrapper<CbbOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("or_ur_id", userId);
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("or_status", status);
        }
        if (!StringUtils.isEmpty(key)) {
            wrapper.like("or_sp_name", key);
        }
        List<CbbOrderEntity> list = list(wrapper);
        for (CbbOrderEntity cbbOrderEntity : list) {
            cbbOrderEntity.setCbbSortEntity(cbbSortService.getById(cbbOrderEntity.getOrStId()));
        }
        return list;
    }

    @Override
    public void saveOrder(CbbOrderEntity cbbOrder) {
        cbbOrder.setOrCreateTime(new Date());
        cbbOrder.setOrCount(1);
        cbbOrder.setOrStatus(0);
        save(cbbOrder);
    }

    @Override
    public PageUtils queryPagePhone(Map<String, Object> params) {
        QueryWrapper<CbbOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("or_phone", params.get("phone"));
        IPage<CbbOrderEntity> page = this.page(
                new Query<CbbOrderEntity>().getPage(params),
                wrapper
        );
        // System.out.println(page.getSize());
        return new PageUtils(page);
    }

    @Override
    public boolean saveOrderPay(CbbOrderEntity cbbOrder) {
        cbbOrder.setOrCreateTime(new Date());
        cbbOrder.setOrPlayTime(new Date());
        //商品的销量加1；
        String Key = "shop_xl_" + cbbOrder.getOrStId();
        //获取锁
        RLock lock = redisson.getLock(Key);
        //设置锁的过期时间  如果设置失败就会停留在这
        lock.lock(3, TimeUnit.MINUTES);

        //第一步查询秒杀商品是不是还在秒杀中
        try {
            CddShopEntity shop = shopService.getById(cbbOrder.getOrStId());
            shop.setSpSales(shop.getSpSales() + cbbOrder.getOrCount());
            shopService.updateById(shop);
            // System.out.println("秒杀成功当前库存:" + integer);
            return save(cbbOrder);
        } finally {
            lock.unlock();
        }
    }

    @Override
    public CbbOrderEntity infoOrder(String orUuid) {
        CbbOrderEntity orderEntity = getById(orUuid);
        orderEntity.setUser(cbbUserService.getById(orderEntity.getOrUrId()));
        return orderEntity;
    }

}